
package com.woniuxy.ams.service.impl;

import com.woniuxy.ams.dao.AppraisalTemplateDao;
import com.woniuxy.ams.dao.DeptDao;
import com.woniuxy.ams.dao.TemplateIdDao;
import com.woniuxy.ams.dao.UserDao;
import com.woniuxy.ams.entity.*;
import com.woniuxy.ams.service.AppraisalTemplateSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 部门考核模板的serivceImpl实现层
 */
@Service
public class AppraisalTemplateSericeImpl implements AppraisalTemplateSerice {
    @Autowired(required = false)
    AppraisalTemplateDao appraisalTemplateDao;
    @Autowired(required = false)
    TemplateIdDao templateIdDao;
    @Autowired(required = false)
    UserDao userDao;
    @Resource
    private DeptDao deptDao;

    //查询考核模板
    @Override
    public List<AppraisalTemplate> findAllTemplate(Dept dept) {
        return appraisalTemplateDao.findAllTemplate(dept);
    }

    //查询一条模板
    @Override
    public AppraisalTemplate findTemplate(int templateId) {
        return appraisalTemplateDao.findTemplate(templateId);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updateNewTemplate(NewTemplate newTemplate) {
        Dept dept = deptDao.findOneByName(newTemplate.getDepartment());
        newTemplate.setDepartmentId(dept.getId());
        AppraisalTemplate appraisalTemplate = new AppraisalTemplate();
        String type = newTemplate.getType();
        String templateName = newTemplate.getTemplateName();
        String department = newTemplate.getDepartment();
        int departmentId = newTemplate.getDepartmentId();
        int id = newTemplate.getId();
        appraisalTemplate.setType(type);
        appraisalTemplate.setTemplateName(templateName);
        appraisalTemplate.setDepartment(department);
        appraisalTemplate.setDepartmentId(departmentId);
        appraisalTemplate.setId(id);
        return appraisalTemplateDao.updateTemplate(appraisalTemplate);
    }

    // 模板搜索内容
    @Override
    public List<AppraisalTemplate> findAllTemplateBySearch(ExamineListFormInline examineListFormInline) {
        return appraisalTemplateDao.findAllTemplateBySearch(examineListFormInline);
    }

    @Override
    public int deleteTemplate(AppraisalTemplate appraisalTemplate) {
        return appraisalTemplateDao.deleteTemplate(appraisalTemplate);
    }
    //新增考核项
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int insertNewTemplate(NewTemplate newTemplate) {
        //模板插入返回值
        int i;
        if (newTemplate.getId() > 0 ) {
            updateNewTemplate(newTemplate);
            return 1;
        }else {
            // 创建一个考核模板表对象
            Dept dept = deptDao.findOneByName(newTemplate.getDepartment());
            newTemplate.setDepartmentId(dept.getId());
            AppraisalTemplate appraisalTemplate = new AppraisalTemplate();
            String type = newTemplate.getType();
            String templateName = newTemplate.getTemplateName();
            String department = newTemplate.getDepartment();
            int departmentId = newTemplate.getDepartmentId();
            appraisalTemplate.setType(type);
            appraisalTemplate.setTemplateName(templateName);
            appraisalTemplate.setDepartment(department);
            appraisalTemplate.setDepartmentId(departmentId);
            i = appraisalTemplateDao.insertTemplate(appraisalTemplate);
            int id = appraisalTemplate.getId();
            // 获取模板中间表对象中考核项id，创建考核中间表对象（多个）
            List<TemplateAssess> templateAssess = newTemplate.getTemplateAssess();
            templateIdDao.insert(id, templateAssess);
            List<RaterUser> raterUser = newTemplate.getRaterUser();
            // 创建此模板中评分人及其占比
            for (RaterUser raterUser1 : raterUser) {
                raterUser1.setDepartmentId(departmentId);
                raterUser1.setTemplateId(id);
                BigDecimal bignum5 = new BigDecimal((raterUser1.getRaterUserWeightId() / 100)).multiply(new BigDecimal(raterUser1.getWeight()));
                double weight = Math.floor(bignum5.doubleValue() * 1000) / 1000;
                raterUser1.setRaterUserWeightId(weight);
                User oneByName = userDao.findOneByName(raterUser1.getRater());
                Integer id1 = oneByName.getId();
                String name = oneByName.getName();
                raterUser1.setRater(name);
                raterUser1.setRaterUserid(id1);
            }
            appraisalTemplateDao.insertRaterUser(raterUser);
        }
        return i;
    }


}